/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaFinalView.java
 *
 * Created on 28/09/2009, 21:00:43
 */

package view.processo;

import model.processo.FinalizacaoData;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Administrador
 */
public class ListaFinalView extends javax.swing.JDialog {

    /** Creates new form ListaFinalView */
    public ListaFinalView(java.awt.Frame parent, boolean modal, AtividadeView at) {
        super(parent, modal);
        atividade = at;
        finaldata = new FinalizacaoData(true);
        initComponents();
        jtbfinalizacoes.getSelectionModel().addListSelectionListener(selectionListener);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbfinalizacoes = new javax.swing.JTable();
        jtpesquisar = new javax.swing.JTextField();
        jlpesquisar = new javax.swing.JLabel();
        jtbcancel = new javax.swing.JButton();
        jtbok = new javax.swing.JButton();

        setTitle("Finalizações");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jtbfinalizacoes.setModel(finaldata);
        jtbfinalizacoes.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        jtbfinalizacoes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtbfinalizacoes.setShowVerticalLines(false);
        jtbfinalizacoes.getTableHeader().setReorderingAllowed(false);
        jtbfinalizacoes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbfinalizacoesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbfinalizacoes);

        jtpesquisar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtpesquisarKeyReleased(evt);
            }
        });

        jlpesquisar.setText("Pesquisar :");

        jtbcancel.setText("Cancelar");
        jtbcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbcancelActionPerformed(evt);
            }
        });

        jtbok.setText("OK");
        jtbok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbokActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlpesquisar)
                    .addComponent(jtpesquisar, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jtbok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtbcancel)))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(267, Short.MAX_VALUE)
                .addComponent(jlpesquisar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtpesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtbok)
                    .addComponent(jtbcancel))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(85, Short.MAX_VALUE)))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-383)/2, 416, 383);
    }// </editor-fold>//GEN-END:initComponents

    private void jtpesquisarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtpesquisarKeyReleased
        TableRowSorter<TableModel> sorter;
        sorter = new TableRowSorter<TableModel>(jtbfinalizacoes.getModel());
        jtbfinalizacoes.setRowSorter(sorter);
        sorter.setRowFilter(javax.swing.RowFilter.regexFilter(jtpesquisar.getText(),1));
    }//GEN-LAST:event_jtpesquisarKeyReleased

    private void jtbcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbcancelActionPerformed
            this.dispose();
    }//GEN-LAST:event_jtbcancelActionPerformed

    private void jtbokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbokActionPerformed
            int linha = jtbfinalizacoes.getSelectedRow();
            finaly = finaldata.getFinalizacaoId(Integer.valueOf(String.valueOf(jtbfinalizacoes.getValueAt(linha, 0))));
            int idativ = Integer.valueOf(atividade.jtcodigo.getText());
            int idfin = finaly.getId();
            atividade.insereAtivFin(idativ, idfin);
            this.dispose();
    }//GEN-LAST:event_jtbokActionPerformed

    private void jtbfinalizacoesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbfinalizacoesMouseClicked

        if(evt.getClickCount() == 2)
        {
            jtbok.doClick();
        }
    }//GEN-LAST:event_jtbfinalizacoesMouseClicked

    /**
    * @param args the command line arguments
    */

    private ListSelectionListener selectionListener = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
            

        }
    };

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ListaFinalView dialog = new ListaFinalView(new javax.swing.JFrame(), true, new AtividadeView());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlpesquisar;
    private javax.swing.JButton jtbcancel;
    private javax.swing.JTable jtbfinalizacoes;
    private javax.swing.JButton jtbok;
    private javax.swing.JTextField jtpesquisar;
    // End of variables declaration//GEN-END:variables
    private model.processo.FinalizacaoData finaldata;
    private model.processo.Finalizacao finaly;
    private AtividadeView atividade;
}
